package adventure.commands;

import adventure.*;
import player.*;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class Recall extends AbstractCommand {

    public Recall(Adventure a) {
        super(a, new String[]{"RECALL"});
    }

    @Override
    public String validateAndExecute(Player p, String[] args) {
        if (args.length == 1) {
            return recall(p);
        } else {
            return "Command not recognised.";
        }
    }

    private String recall(Player player) {
        if (!player.inCombat()) {
            int specialNeeded = Math.max(player.getMaxSpecial() / 4,
                    player.getMaxSpecial() / 2 - player.getIntelligence());

            if (player.getSpecial() >= specialNeeded) {
                out.broadcastLocalToPlayer(player,
                        ColourUtil.colourise(player.getAliveName(), ColourUtil.magenta)
                        + " has returned to their spawn location!");

                rooms.leaveRoom(player);
                player.recall();
                rooms.enterRoom(player);
                out.broadcastLocalToPlayer(player,
                        ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                        + " has returned.");

                String output = "You have returned to your spawn location!\n\n";
                output += adv.printRoom(player);

                return output;
            } else {
                return "You need at least " + specialNeeded + " special to recall!";
            }
        } else {
            return "You cannot recall in combat!";
        }
    }
}
